package org.exria.facevook.locator;

import java.util.StringTokenizer;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;

import org.exria.framework.ApplicationLocator;
import org.exria.framework.BusinessObjectMetadata;

public class ExriaLocator implements ApplicationLocator {
	private final String DOT = ".";
	private final String SLASH = "/";
	private final String DEFAULT_PACKAGENAME = "org.exria.facevook";
	private final String PKGCONTROLLER = "controller";
	private final String NAMECONTROLLER = "Controller";
	
	private String getURL(String uri) {
		String packageName = uri.substring(uri.lastIndexOf(SLASH) + 1, uri.length());
		StringTokenizer pkgToken = new StringTokenizer(packageName, DOT);
		int cnt = 0;
		int tknLen = pkgToken.countTokens() - 1;

		StringBuffer pkgBuffer = new StringBuffer();
		pkgBuffer.append(DEFAULT_PACKAGENAME);
		pkgBuffer.append(DOT);

		String tmpTkn = null;
		while (pkgToken.hasMoreElements()) {
			tmpTkn = pkgToken.nextElement().toString();
			cnt++;

			if (cnt < tknLen) {
				pkgBuffer.append(tmpTkn);
				pkgBuffer.append(DOT);
			} else if (cnt == tknLen) {
				pkgBuffer.append(PKGCONTROLLER);
				pkgBuffer.append(DOT);
				pkgBuffer.append(tmpTkn);
			} else {
				pkgBuffer.append(NAMECONTROLLER);
			}
		}
		return pkgBuffer.toString();
	}

	public BusinessObjectMetadata getBusinessObjectMetadata(
			ServletConfig config, HttpServletRequest request) {

		final String uri = request.getRequestURI();
		final String method = request.getParameter("method");
		System.out.println("=123123==> " + getURL(uri));
		System.out.println("된다 된다 된다 힘쓰면 된다.................");
		BusinessObjectMetadata bom = new BusinessObjectMetadata(getURL(uri), method);
		return bom;
	}
}
